package top.gbyun.engine.actor;

import akka.actor.AbstractActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import top.gbyun.core.entity.NodeInfo;

import java.util.Optional;

/**
 * @author gengbin
 * @date 2024-12-19-18:22
 */
public abstract class AbstractNodeActor extends AbstractActor {

    private final NodeInfo nodeInfo;

    private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);

    public AbstractNodeActor(NodeInfo nodeInfo) {
        this.nodeInfo = nodeInfo;
    }


    @Override
    public void preStart() throws Exception {
        // 1.构建节点路由

        log.info("节点[{}-{}-{}]已经启动", nodeInfo.getType(), nodeInfo.getId(), nodeInfo.getName());
    }

    @Override
    public void preRestart(Throwable reason, Optional<Object> message) throws Exception {
        log.info("节点[{}-{}-{}]即将重启", nodeInfo.getType(), nodeInfo.getId(), nodeInfo.getName());
    }

    @Override 
    public void postRestart(Throwable reason) throws Exception {
        log.info("节点[{}-{}-{}]重启完毕", nodeInfo.getType(), nodeInfo.getId(), nodeInfo.getName());
    }

    @Override
    public void postStop() throws Exception {
        log.info("节点[{}-{}-{}]已被销毁", nodeInfo.getType(), nodeInfo.getId(), nodeInfo.getName());
    }


    /**
     * 消息接收器
     * @author gengbin
     * @date 2024/12/19 14:58
     * @return akka.actor.AbstractActor.Receive
     */
    @Override
    public Receive createReceive() {
        return receiveBuilder().matchAny(this::unhandled).build();
    }


    public abstract void onCreate();

    public abstract void onMessage(Object message);

    public abstract void onDestroy();
}
